﻿using FastData.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FastData
{
    internal class SelectQueryProvider<T> : IQueryProvider
    {
        private DataAccess _data;

        internal SelectQueryProvider(DataAccess data)
        {
            _data = data;
        }

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return new SelectQuery<TElement>(this, expression);
        }

        public IQueryable CreateQuery(Expression expression)
        {
            Type elementType = expression.Type.GetElementType();
            return (IQueryable)Activator.CreateInstance(typeof(SelectQuery<>).MakeGenericType(elementType), new object[] { this, expression });
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return new SelectQueryTranslator<TResult>(_data).TranslateSingle(expression);
        }

        public object Execute(Expression expression)
        {
            return new SelectQueryTranslator<T>(_data).Translate(expression);
        }

        internal async Task<IEnumerable<T>> ExecuteAsync(Expression expression)
        {
            return await new SelectQueryTranslator<T>(_data).TranslateAsync(expression);
        }
    }
}
